package com.taskcollection.service;

import com.taskcollection.bean.TeacherBean;
import com.taskcollection.serviceInterface.TeacherServiceInterface;
import com.taskcollection.util.DBConnection;
import com.taskcollection.util.DBUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/7/17 0017.
 */
public class TeacherService implements TeacherServiceInterface{
    /**
     * 教师注册
     * 姓名@param name
     * 手机号@param phone
     * 密码@param passwd
     * 是否注册成功@return
     */
    public boolean reg(TeacherBean tea){
        return ServiceUtil.reg(tea);
    }
    public TeacherBean login(String phone,String passwd){
        Connection conn = DBConnection.getinstance().getConnection();
        String sql = "SELECT * FROM teacherinfo WHERE teacher_phone=? and teacher_passwd=?";
        PreparedStatement ps = null;
        ResultSet rs = null;
        TeacherBean tea = new TeacherBean();
        try{
            ps = conn.prepareStatement(sql);
            ps.setString(1,phone);
            ps.setString(2,passwd);
            rs = ps.executeQuery();
            if(rs.next()){
                tea.setTeacher_id(rs.getString("Teacher_id"));
                tea.setTeacher_phone(rs.getString("teacher_phone"));
                tea.setTeacher_name(rs.getString("teacher_name"));
                tea.setTeacher_gender(rs.getString("teacher_gender"));
                tea.setTeacher_contact(rs.getString("teacher_contact"));
            }
            return tea;
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            DBUtil.closeResultSet(rs);
            DBUtil.closePreparedStatement(ps);
            DBUtil.closeConnection(conn);
        }
        System.out.println("执行return了null");
        return null;
    }
    public boolean checkLogin(String phone,String passwd){
        TeacherBean tea = login(phone,passwd);
        System.out.println(phone+"**"+passwd+"**"+tea.getTeacher_id());
        if("".equals(tea.getTeacher_id())||tea.getTeacher_id()==null){
            return false;
        }else{
            return true;
        }
    }
    public  boolean checkPhone(String phone) {
        return ServiceUtil.checkPhone("teacherinfo",phone);
    }
    public TeacherBean getById(String id){
        String sql = "select * from studentinfo where Student_id=?";
        Connection conn = DBConnection.getinstance().getConnection();
        PreparedStatement ps = null;
        ResultSet rs = null;
        TeacherBean tea = new TeacherBean();
        try{
            ps=conn.prepareStatement(sql);
            ps.setString(1,id);
            rs = ps.executeQuery();
            if(rs.next()){
                tea.setTeacher_id(rs.getString("Teacher_id"));
                tea.setTeacher_phone(rs.getString("teacher_phone"));
                tea.setTeacher_name(rs.getString("teacher_name"));
                tea.setTeacher_gender(rs.getString("teacher_gender"));
                tea.setTeacher_contact(rs.getString("teacher_contact"));
            }
            return tea;
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            DBUtil.closeResultSet(rs);
            DBUtil.closePreparedStatement(ps);
            DBUtil.closeConnection(conn);
        }
        return null;
    }
    public List<TeacherBean> getAll(){
        List<TeacherBean> tbList = new ArrayList<TeacherBean>();
        String sql = "select * from teacherinfo";
        Connection conn = DBConnection.getinstance().getConnection();
        PreparedStatement ps = null;
        ResultSet rs = null;
        try{
            ps = conn.prepareStatement(sql);
            rs = ps.executeQuery();
            while (rs.next()){
                TeacherBean tb = new TeacherBean();
                tb.setTeacher_id(rs.getString("Teacher_id"));
                tb.setTeacher_phone(rs.getString("teacher_phone"));
                tb.setTeacher_name(rs.getString("teacher_name"));
                tb.setTeacher_gender(rs.getString("teacher_gender"));
                tb.setTeacher_contact(rs.getString("teacher_contact"));
                tbList.add(tb);
            }
            return tbList;
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            DBUtil.closeResultSet(rs);
            DBUtil.closePreparedStatement(ps);
            DBUtil.closeConnection(conn);
        }
        return null;
    }
}
